package com.oig.sys.minio.core;

import io.minio.messages.Item;
import io.minio.messages.Owner;
import lombok.Data;

import java.time.LocalDate;

@Data
public class MinIOItem {
    /**对象名称**/
    private String objectName;
    /**最后操作时间**/
    private LocalDate lastModified;

    private String etag;
    /**对象大小**/
    private String size;
    private String storageClass;
    private Owner owner;
    /**对象类型：directory（目录）或file（文件）**/
    private String type;


    public MinIOItem(Item item) {
        this.objectName = item.objectName();
        this.type = item.isDir() ? "directory" : "file";
        this.etag = item.etag();
        long sizeNum = item.size();
        this.size = sizeNum > 0 ? convertFileSize(sizeNum):"0";
        this.storageClass = item.storageClass();
        this.owner = item.owner();
        try {
            this.lastModified = item.lastModified().toLocalDate();
        }catch(NullPointerException e){}
    }


    public String convertFileSize(long size) {
        long kb = 1024;
        long mb = kb * 1024;
        long gb = mb * 1024;
        if (size >= gb) {
            return String.format("%.1f GB", (float) size / gb);
        } else if (size >= mb) {
            float f = (float) size / mb;
            return String.format(f > 100 ? "%.0f MB" : "%.1f MB", f);
        } else if (size >= kb) {
            float f = (float) size / kb;
            return String.format(f > 100 ? "%.0f KB" : "%.1f KB", f);
        } else{
            return String.format("%d B", size);
        }
    }
}
